<?php

include ("configs/config.php");

//if ( $_SESSION['LOGIN_NAME'] <> "admin" ) 
//	{
//	header("Location: login-admin.php");
//	exit();
//	}
$tpl = new XTemplate("./template/tpl_admin.html");
$tpl->parse("main.body.menu");
  
  



if( $reset_counter==1 )
			{//we reset the counter
				$query_url="update incoming_links set new_views=null";
				$request_url=mysql_query($query_url);
				$query_url="update redirected_links set new_views=null";
				$request_url=mysql_query($query_url);
				
			//and register the reset time
				$date=date('Y.m.d');
				$query_time_reset="UPDATE reset_time SET last_reset='$date'";										
			  	$request_time_reset=mysql_query($query_time_reset);												
			}
			
              $query_time_reset="SELECT * FROM reset_time";//we find the last reset time			
			  $request_time_reset=mysql_query($query_time_reset);			 
			  $reset_t=mysql_fetch_array($request_time_reset);
			  $reset_time=$reset_t['last_reset'];			 
			  $tpl->assign("reset_time",$reset_time);
			  
			  

//find all url`s in sorted order
	
	if($sort_url==2)//on in
		{
		$query_url="SELECT url_address . * , incoming_links.incoming_views FROM url_address LEFT JOIN incoming_links ON incoming_links.url = url_address.url GROUP BY url ORDER BY incoming_views DESC ";
		}
	elseif($sort_url==3)//on out
		{
		$query_url="SELECT url_address. * , sum( redirected_links.views ) as views FROM url_address LEFT JOIN redirected_links ON redirected_links.url_site = url_address.url GROUP BY url_address.url ORDER BY views DESC ";			
		}		
	elseif($sort_url==4)//on new_in
		{
		$query_url="SELECT url_address . * , incoming_links.new_views FROM url_address LEFT JOIN incoming_links ON incoming_links.url = url_address.url GROUP BY url ORDER BY new_views DESC ";					
		}
	elseif($sort_url==5)//on new_out
		{
		$query_url="SELECT url_address. * , sum( redirected_links.new_views ) as new_views FROM url_address LEFT JOIN redirected_links ON redirected_links.url_site = url_address.url GROUP BY url_address.url ORDER BY new_views DESC ";								
		}
	else//on url
		{
		$query_url="SELECT url FROM url_address order by url";						
		}
		
	$request_url=mysql_query($query_url);
	



while($thisUrl=mysql_fetch_array($request_url)) //for every url we find: 
{
	
	$query_in="select incoming_views, new_views from incoming_links where url='" .$thisUrl['url']."'";//cliks in
	 
	$request_in=mysql_query($query_in);
	$thisU=@mysql_fetch_array($request_in);
	if($thisU['incoming_views']==""){$no_url_in=0;}
	else {$no_url_in=$thisU['incoming_views'];}	
	if($thisU['new_views']==""){$new_no_url_in=0;}
	else {$new_no_url_in=$thisU['new_views'];}	
		
	
	$query_out="select sum(views) views, sum(new_views) as new_views from redirected_links where url_site='".$thisUrl['url']."'";//click out
	$request_out=mysql_query($query_out);	
	$thisU=@mysql_fetch_array($request_out);
	if($thisU['views']==""){$no_url_out=0;}
	else {$no_url_out=$thisU['views'];}
	if($thisU['new_views']==""){$new_no_url_out=0;}
	else {$new_no_url_out=$thisU['new_views'];}



	if($no_url_in!=0&&$no_url_out!=0)
	{
		if($no_url_in>$no_url_out)//if in>out...
		{
			$rez=$no_url_in/$no_url_out;		
			$percent=round(100*$rez,0)."%";	
		
		}
		else
		{		
			$rez=$no_url_out/$no_url_in;	
			$percent=round(100*$rez,0)."%";	
		}
		
	}	
	elseif($no_url_in==0)
	{
		$percent="No Incoming";					
	}
	elseif($no_url_out==0)
	{
		$percent="No Outgoing";						
	}
	else
	{
		$percent="No Clicks";		
	}
	
//----------------------	
	if($new_no_url_in!=0&&$new_no_url_out!=0) 
	{
		if($new_no_url_in>$new_no_url_out)//if in>out...
		{
			$new_rez=$new_no_url_in/$new_no_url_out;		
			$new_percent=round(100*$rez,0)."%";	
		
		}
		else
		{		
			$new_rez=$new_no_url_out/$new_no_url_in;	
			$new_percent=round(100*$rez,0)."%";	
		}
		
	}	
	elseif($new_no_url_in==0 && $new_no_url_out!=0)
	{
		$new_percent="No Incoming";					
	}
	elseif($new_no_url_out==0 && $new_no_url_in!=0)
	{
		$new_percent="No Outgoing";						
	}
	else
	{
		$new_percent="No Clicks";		
	}
	
	
	
		
		
		
		
		if($thisUrl['url']=="unknown_url"){$thisUrl['url']="unknown_url_or_the_same";}
		$tpl->assign("site",$thisUrl['url']);
		$tpl->assign("in",$no_url_in);
		$tpl->assign("new_in",$new_no_url_in);
		$tpl->assign("out",$no_url_out);
		$tpl->assign("new_out",$new_no_url_out);
		$tpl->assign("ratio",$percent);		
		$tpl->assign("new_ratio",$new_percent);
	$tpl->parse("main.body.in_links.row");			
}//while	



$reset_counter=0;









$tpl->parse("main.body.in_links");
$tpl->parse("main.body");

$tpl->parse("main");
$tpl->out("main");
?>
